package com.xqboss.web.controller.punch;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.convert.punch.PunchUserRecordConverter;
import com.xqboss.apps.domain.punch.PunchUserRecord;
import com.xqboss.apps.dto.PageDto;
import com.xqboss.apps.dto.punch.EditPunchConfigDto;
import com.xqboss.apps.dto.punch.GetWebPunchRecordDto;
import com.xqboss.apps.service.nft.NftNumberService;
import com.xqboss.apps.service.nft.NftService;
import com.xqboss.apps.service.punch.PunchUserRecordService;
import com.xqboss.apps.service.sys.ConfigService;
import com.xqboss.apps.vo.punch.UserPunchRecordVo;
import com.xqboss.apps.vo.punch.WebUserPunchRecordVo;
import com.xqboss.apps.vo.user.UserNFTItemVo;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Trent
 * @date 2023/9/5
 */
@Api(tags = {"打卡-打卡管理"})
@RestController("WebUserPunchController")
@RequestMapping("/web/punch")
public class UserPunchController {

    @Autowired
    private PunchUserRecordService punchUserRecordService;

    @Autowired
    private PunchUserRecordConverter punchUserRecordConverter;

    @Autowired
    private NftNumberService nftNumberService;

    @Autowired
    private ConfigService configService;

    @PreAuthorize("@ss.hasPermi('web:punch:record:list')")
    @ApiOperation("获取用户打卡记录")
    @GetMapping("record/list")
    public R<List<WebUserPunchRecordVo>> recordList(@Validated GetWebPunchRecordDto pageInfo){
        pageInfo.startPageAndOrder();
        List<PunchUserRecord> list = punchUserRecordService.getUserPunchRecordList(pageInfo);
        List<WebUserPunchRecordVo> voList = punchUserRecordConverter.toWebUsrePunchRecordVoList(list);
        for (WebUserPunchRecordVo recordVo : voList) {
            UserNFTItemVo nftInfo = nftNumberService.selectNftInfo(recordVo.getNftNumberId());
            recordVo.setNftInfo(nftInfo);
        }
        return R.ok(voList, list);
    }

    @PreAuthorize("@ss.hasPermi('web:punch:config:update')")
    @ApiOperation("设置打卡系数配置")
    @PutMapping("config/update")
    @Log(title = "打卡配置", businessType = BusinessType.UPDATE)
    public R updateConfig(@Validated @RequestBody EditPunchConfigDto dto){
        configService.updatePunchConfig(dto);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:punch:config:info')")
    @ApiOperation("获取打卡系数配置")
    @GetMapping("config/info")
    public R<EditPunchConfigDto> updateConfig(){
        EditPunchConfigDto dto = configService.getUpdateConfig();
        return R.ok(dto);
    }
}
